<template>
  <Layout style="height: 100%" class="main">
    <Header class="header-con">
      <header-bar :collapsed="collapsed">
        <user
          :message-unread-count="unreadCount"
          :user-avator="userAvator"
          :user-name="userInfo.name"
          :user-type="userInfo.userType"
        />
      </header-bar>
    </Header>

    <Layout style="height: calc(100% - 64px)">
      <Sider
        hide-trigger
        collapsible
        :width="200"
        :collapsed-width="64"
        v-model="collapsed"
        class="left-sider"
        :style="{ overflow: 'hidden' }"
      >
        <side-menu
          accordion
          ref="sideMenu"
          :active-name="$route.name"
          :collapsed="collapsed"
          @on-select="turnToPage"
          :menu-list="menuList"
          @on-coll-change="collapsed = !collapsed"
        >
        </side-menu>
      </Sider>
      <Content class="main-content-con">
        <Layout class="main-layout-con">
          <div class="tag-nav-wrapper">
            <tags-nav
              :value="$route"
              @input="handleClick"
              :list="tagNavList"
              @on-close="handleCloseTag"
            />
          </div>
          <Content class="content-wrapper">
            <!-- <transition name="fade-transform" mode="out-in">
              <keep-alive>
                <router-view v-if="$route.meta.keepAlive"></router-view>
              </keep-alive>
            </transition>
            <transition name="fade-transform" mode="out-in">
              <router-view
                v-if="!$route.meta.keepAlive"
                :key="key"
              ></router-view>
            </transition> -->
            <!-- <transition name="fade-transform" mode="out-in"> -->
            <!-- <router-view></router-view> -->
            <!-- </transition> -->
            <transition name="fade-transform" mode="out-in">
              <keep-alive :include="cacheList">
                <router-view :key="key" />
              </keep-alive>
            </transition>
          </Content>
        </Layout>
      </Content>
    </Layout>
  </Layout>
</template>
<script>
import SideMenu from '%/modules/home/side-menu'
import HeaderBar from '%/modules/home/header-bar'
import TagsNav from '%/modules/home/tags-nav'
import User from '%/modules/home/user'
import { mapMutations, mapActions } from 'vuex'
import {
  getNewTagList,
  routeEqual,
  getMenuInLocalstorage,
  getUserInLocalstorage
} from '@/libs/util'
import routers from '@/router/routers'
import minLogo from '@/assets/images/logo-mini.png'
import maxLogo from '@/assets/images/logo-max.png'

export default {
  name: 'Home',
  components: {
    SideMenu,
    HeaderBar,
    TagsNav,
    User
  },
  data() {
    return {
      collapsed: false,
      minLogo,
      maxLogo,
      menuList: [],
      userInfo: {}
    }
  },
  computed: {
    // keep-alive默认是按照组件的name缓存的，例如/detail/:1和/detail/:2这种vue缓存的是detail组件，（就好比类与类实例的概念，类是公共的，实例是在类基础上构建出来的，组件：类，页面：类实例）所以这两个页面展示的内容是同一个，如果要展示各自的，可以给router-view添加一个key，刚开始添加的是path + 时间戳，这样子详情显示对了，但是配置的缓存组件就不缓存内容了，因为每一次打开的key与之前一次不一样，所以vue认为这是一个新的页面不进行缓存，所以结论就是：1）缓存页面的key多次打开结果必须一样；2）不缓存界面比如详情每次打开key要不一样。详情界面的参数是通过path传递的，所以path必然不一样，所以key改为route的path可以同时满足以上两个要求。
    key() {
      return this.$route.path
    },
    tagNavList() {
      return this.$store.state.app.tagNavList
    },
    tagRouter() {
      return this.$store.state.app.tagRouter
    },
    userAvator() {
      return this.$store.state.user.avatorImgPath
    },
    cacheList() {
      const list = [
        'ParentView',
        ...(this.tagNavList.length
          ? this.tagNavList
              .filter(item => !(item.meta && item.meta.notCache))
              .map(item => item.name)
          : [])
      ]
      return list
    },
    unreadCount() {
      return this.$store.state.user.unreadCount
    }
  },
  methods: {
    ...mapMutations(['setTagNavList', 'addTag', 'setHomeRoute', 'closeTag']),
    ...mapActions(['handleLogin']),
    turnToPage(route) {
      let { name, params, query } = {}
      if (typeof route === 'string') name = route
      else {
        name = route.name
        params = route.params
        query = route.query
      }
      this.$router.push({
        name,
        params,
        query
      })
      if (route.name) {
        return
      } else {
        if (route != 'home') {
          let data = {
            menuHref: route
          }
          this.$api.homeConterller.recentlyUseAdd(data).then(res => {})
        }
      }
    },
    handleCloseTag(res, type, route) {
      if (type !== 'others') {
        if (type === 'all') {
          this.turnToPage(this.$config.homeName)
        } else {
          if (routeEqual(this.$route, route)) {
            this.closeTag(route)
          }
        }
      }
      this.setTagNavList(res)
    },
    handleClick(item) {
      this.turnToPage(item)
    },
    findFirstShow(list) {
      if (list.length > 0) {
        let first = list[0]
        if (first.children && first.children.length > 0) {
          return this.findFirstShow(first.children)
        } else {
          return first.name
        }
      }
    }
  },
  watch: {
    $route(newRoute) {
      const { name, query, params, meta } = newRoute
      // this.addTag({
      //   route: { name, query, params, meta },
      //   type: 'push'
      // })
      this.$store.dispatch('AddUniqueTag', {
        route: { name, query, params, meta },
        type: 'push'
      })
      this.setTagNavList(getNewTagList(this.tagNavList, newRoute))
      this.$refs.sideMenu.updateOpenName(newRoute.name)
    }
  },
  created() {
    //获取左侧菜单
    this.menuList = getMenuInLocalstorage()
    this.setTagNavList()
    this.setHomeRoute(routers)
    const { name, params, query, meta } = this.$route
    this.addTag({
      route: { name, params, query, meta }
    })
    // 如果当前打开页面不在标签栏中，跳到homeName页
    if (!this.tagNavList.find(item => item.name === this.$route.name)) {
      this.$router.push({
        name: this.$config.homeName
      })
    }
    this.userInfo = getUserInLocalstorage()
  }
}
</script>
<style lang="scss">
.main {
  .logo-con {
    height: 64px;
    padding: 10px;
    img {
      height: 44px;
      width: auto;
      display: block;
      margin: 0 auto;
    }
  }
  .header-con {
    background-color: #003b98;
    padding: 0 20px;
    width: 100%;
  }
  .main-layout-con {
    height: 100%;
    overflow: hidden;
    background: #f0f0f0;
  }

  .main-content-con {
    height: 100%;
    overflow: hidden;
  }
  .tag-nav-wrapper {
    padding: 0;
    height: 40px;
    background: #f0f0f0;
  }
  .content-wrapper {
    margin: 0 14px 14px;
    height: calc(100% - 80px);
    overflow: auto;
    background-color: #ffffff;
  }
  .left-sider {
    // background: #fff;
    .ivu-layout-sider-children {
      overflow-y: scroll;
      margin-right: -18px;
      background: #fff;
    }
  }
  .tab-page {
    text-align: center;
    margin-top: 20px;
  }
  /* fade-transform */
  .fade-transform-leave-active,
  .fade-transform-enter-active {
    transition: all 0.5s;
  }

  .fade-transform-enter {
    opacity: 0;
    transform: translateX(-30px);
  }

  .fade-transform-leave-to {
    opacity: 0;
    transform: translateX(30px);
  }
}
.content-height {
  padding: 15px 15px 15px 15px;
  // background: #fff;
  border-radius: 3px;
}
.search-class {
  margin-bottom: 20px;
}
.ivu-menu-item > i {
  margin-right: 12px !important;
}
.ivu-menu-submenu > .ivu-menu > .ivu-menu-item > i {
  margin-right: 8px !important;
}
.collased-menu-dropdown {
  width: 100%;
  margin: 0;
  line-height: normal;
  padding: 7px 0 6px 16px;
  clear: both;
  font-size: 12px !important;
  white-space: normal;
  list-style: none;
  cursor: pointer;
  transition: background 0.2s ease-in-out;
  &:hover {
    background: rgba(100, 100, 100, 0.1);
  }
  & * {
    color: #515a6e;
  }
  .ivu-menu-item > i {
    margin-right: 12px !important;
  }
  .ivu-menu-submenu > .ivu-menu > .ivu-menu-item > i {
    margin-right: 8px !important;
  }
}
.button-class {
  color: #0099ff;
  padding: 0 5px;
  cursor: pointer;
  white-space: nowrap;
}

.ivu-select-dropdown.ivu-dropdown-transfer {
  max-height: 400px;
}
.layout-100 {
  height: 100%;
  .header-cont {
    background-color: #ffffff;
    padding: 0 20px;
    border-bottom: 1px solid #e8f7ff;
    font-weight: bold;
  }
  .content-cont {
    background-color: #ffffff;
    height: calc(100% - 0px);
    padding: 20px;
    overflow: auto;
  }
  .footer-cont {
    background-color: #ffffff;
    border-top: 1px solid #e8f7ff;
    text-align: center;
    button {
      margin: 0 10px;
    }
  }
}
</style>
